﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Mastermind : Character {
	
	protected enum Direction
	{
		Top,
		Right,
		Bot,
		Left,
		DiagTopLeft,
		DiagTopRight,
		DiagBotRight,
		DiagBotLeft,
		None
	}

	public float switchSpeed = GameSystem.plateformerMovingSpeed;
	protected Direction direction;
	protected bool switching = false;
	protected Transform rootLocation;
	protected float startTimeMove = 0;
	protected float distanceMove = 0;
	protected Block currentBlock;
	
	// Use this for initialization
	protected override void Start ()
	{
		direction = Direction.None;
		rootLocation = transform;
	}
	
	protected override void Update ()
	{
		SetDirection ();
		Move ();
		Display ();
	}
	
	protected override void OnCollisionEnter2D(Collision2D collision)
	{

	}
	
	protected override void OnCollisionExit2D(Collision2D collision)
	{

	}

	protected void SetDirection()
	{
		if (!switching)
		{
			if ((Input.GetKey (KeyCode.Keypad5) || Input.GetKey (KeyCode.E)) && !switching) { //switch
				if(currentBlock != null && GetComponent<iTween>() == null)
				{
					transform.parent = currentBlock.transform;
					currentBlock.Use();
				}
			}

			if (Input.GetKey (KeyCode.Keypad4)|| Input.GetKey(KeyCode.Q)) { //left
					direction = Direction.Left;
					if (Input.GetKey (KeyCode.Keypad8) || Input.GetKey(KeyCode.Z)) { //diagTopLeft
							direction = Direction.DiagTopLeft;
					} else if (Input.GetKey (KeyCode.Keypad2) || Input.GetKey(KeyCode.S)) { //diagBotLeft
							direction = Direction.DiagBotLeft;
					}
			} else if (Input.GetKey (KeyCode.Keypad8) || Input.GetKey(KeyCode.Z)) { //top
					direction = Direction.Top;
					if (Input.GetKey (KeyCode.Keypad4) || Input.GetKey(KeyCode.Q)) { //diagTopLeft
							direction = Direction.DiagTopLeft;
					} else if (Input.GetKey (KeyCode.Keypad6)|| Input.GetKey(KeyCode.D)) { //diagTopRight
							direction = Direction.DiagTopRight;
					}
			} else if (Input.GetKey (KeyCode.Keypad6) || Input.GetKey(KeyCode.D)) { //right
					direction = Direction.Right;
					if (Input.GetKey (KeyCode.Keypad8) || Input.GetKey(KeyCode.Z)) { //diagTopright
							direction = Direction.DiagTopRight;
					} else if (Input.GetKey (KeyCode.Keypad2) || Input.GetKey(KeyCode.S)) { //diagBotRight
							direction = Direction.DiagBotRight;
					}
			} else if (Input.GetKey (KeyCode.Keypad2) || Input.GetKey(KeyCode.S)) { //bot
					direction = Direction.Bot;
					if (Input.GetKey (KeyCode.Keypad4)|| Input.GetKey(KeyCode.Q)) { //diagbotLeft
							direction = Direction.DiagBotLeft;
					} else if (Input.GetKey (KeyCode.Keypad6)|| Input.GetKey(KeyCode.D)) { //diagBotRight
							direction = Direction.DiagBotRight;
					}
			}
			else
			{
				direction = Direction.None;
			}
		}
	}

	protected Transform GetNearestBlock()
	{
		float visionAngle = GameSystem.visionAngle;
		Vector3 visionDirection = Vector3.zero;
		
		switch (direction)
		{
		case Direction.Bot:
			visionDirection = Vector3.down;
			break;
		case Direction.Left:
			visionDirection = Vector3.left;
			break;
		case Direction.Right:
			visionDirection = Vector3.right;
			break;
		case Direction.Top:
			visionDirection = Vector3.up;
			break;
		case Direction.DiagBotLeft:
			visionDirection = new Vector3(-1f,-1f,0);
			break;
		case Direction.DiagBotRight:
			visionDirection = new Vector3(1f,-1f,0);
			break;
		case Direction.DiagTopLeft:
			visionDirection = new Vector3(-1f,1f,0);
			break;
		case Direction.DiagTopRight:
			visionDirection = new Vector3(1f,1f,0);
			break;
		default:
			break;
		}

//		Vector3 toObject = t.position - transform.position;
//		RaycastHit hit;
//		
//		//Test sommaire si objet cache 
//		if (Physics.Raycast(transform.position, toObject, out hit,m_currentVisionDistance))
//		{		
//			//print("Raycast");
//			//test de nature
//			if(hit.transform == t)
//			{
//				//print("Object");
//				//test d'angle
//				if(Vector3.Dot(toObject.normalized,m_pawnInstance.GetDirection()) > Mathf.Cos(Mathf.Deg2Rad * m_currentVisionAngle/2.0f)) {
//					//print("Angle");
//					return true;
//				}
//			}
//		} 


		Debug.DrawRay(transform.position, visionDirection, Color.green);
	
		Transform nearestTempBlock = this.transform;
		foreach (Object o in Object.FindObjectsOfType<Block>())
		{
			//GameObject go = o as GameObject;
			Block b = o as Block;
			Vector3 targetPosition = b.transform.position;
			Vector3 heading = targetPosition - transform.position;
			heading.Normalize ();

			float dot = Vector3.Dot (heading, visionDirection);

			if (dot > Mathf.Cos (Mathf.Deg2Rad * visionAngle / 2))
			{
				if (nearestTempBlock == this.transform)
				{
					nearestTempBlock = b.transform;
				}
				else if (Vector3.Distance (transform.position, targetPosition) < Vector3.Distance (transform.position, nearestTempBlock.position))
				{
					nearestTempBlock = b.transform;
				}
			}
		}

		return nearestTempBlock;

	}

	protected override void Move()
	{
		if(direction != Direction.None)
		{
			Transform t = GetNearestBlock ();
			currentBlock = t.GetComponent<Block>();
			Move (t);
		}
	}

	protected void Move(Transform moveTo)
	{
		if (moveTo != null)
		{
//			if(!switching)
//			{
//				startTimeMove = Time.time;
//				distanceMove = Vector3.Distance(transform.position,moveTo.position);
//			}
//			switching = true;
//
//			float moveTimer = (Time.time - startTimeMove);
//			float fracTime = (float)Mathf.Min (1, moveTimer / GameSystem.switchTimeInSeconds);
//			transform.position = Vector3.Lerp(transform.position,moveTo.position, fracTime);
//			Debug.Log(fracTime);

			iTween.MoveTo(gameObject, iTween.Hash("position",moveTo.position,"easetype",iTween.EaseType.easeInOutSine,"time",GameSystem.switchTimeInSeconds));

			//if(Vector3.Distance(transform.position, moveTo.position)<0.1f)
			if(transform.position == moveTo.position)
			{
				switching = false;
				rootLocation = transform;
			}
		} 
	}

	public void LooseCollectible()
	{

		if (transform.FindChild ("SpinningAnchor").GetComponent<SpinningAnchor> ().m_anchorChildren.Count > 0)
		{
			GameSystem.plateformer.transform.FindChild("SpinningAnchor").GetComponent<SpinningAnchor>().Add();
			Transform trans = transform.FindChild ("SpinningAnchor").GetComponent<SpinningAnchor> ().m_anchorChildren [0];
			transform.FindChild ("SpinningAnchor").GetComponent<SpinningAnchor> ().m_anchorChildren.Remove (trans);
			Destroy(trans.gameObject);
		}
		GameObject.Find("SoundManager").GetComponent<SoundPlayer>().LaunchCoroutSound((int)(Random.Range(4, 9)), 0f);
	}

	protected override void Display()
	{
	}
}
